local CommonOHOH = _G.CommonOHOH;
local M = CommonOHOH.class.new(script, CommonOHOH.files.BaseObject());
local ConnectClass=_G.CommonOHOH.load.Connect();


local CarEnginePerfab= CommonOHOH:GetPath_Loop("ReplicatedStorage.CarEngine.Base")


-- 初始化一辆车
function M.new(...)
	return M(...);
end;

function M.ctor(self,car,parent)--构造函数
	M.super.ctor(self);
    self.Car=car
    M.super.ctor(self);
end;

function M.dtor(self)
    self:Clear()
	M.super.dtor(self);
end;


function M.Clear(self)
    if self.Model then
        self.Model:Destroy()
        self.Model=nil
    end
end
---放置Humanoid和HumanoidRootPart
---self.CarHumanoid 和-self.CarHumanoidRootPart
function M.CreateHumanoidRootPart(self,newBaseCFrame)

    -- self.CarHumanoid= Instance.new("Humanoid",self.Car:GetModel())
    -- self.CarHumanoid.Archivable = false

    local boundingCFrame, boundingSize =self.Car:GetModel():GetBoundingBox()
    local newSize = Vector3.new(boundingSize.Z,1,boundingSize.X)
    local CarRootPart = Instance.new("Part" )
    CarRootPart.Size = newSize
    CarRootPart.Color = Color3.fromRGB(255, 0, 0)  -- 设置颜色
    CarRootPart.Transparency = 1
	CarRootPart.Name = "HumanoidRootPart"
	CarRootPart.CanTouch = false -- 设置Part可触摸
    CarRootPart.CanCollide = false  -- 禁止碰撞
    CarRootPart.Massless = false -- 设置Massless为false
    -- CarRootPart.Anchored = true -- 确保Part不被固定
    CarRootPart:PivotTo(newBaseCFrame)
    CarRootPart.Parent = self.Car:GetModel()
    self:Weld2Part(CarRootPart,self.Model.Car)
    CarRootPart.Anchored = false 
    self.CarHumanoidRootPart =CarRootPart

    self.Car:GetModel().PrimaryPart = self.CarHumanoidRootPart
end

function M.CreateBody(self,pivot)
    local ringLocate = pivot
    local ringPosition = ringLocate.Position
    local newBaseCFrame = CFrame.new(ringPosition) * CFrame.Angles(CarEnginePerfab:GetPivot():ToEulerAnglesXYZ())
    self:Clear()
    self.Model= CarEnginePerfab:Clone()
    self.Model.Name="Engine"
    self.Model.Parent=  self.Car:GetModel()
    self.Model:PivotTo(newBaseCFrame )
    self.Model.Car.Anchored=true --默认锚固，由外部开启
    self:SetTransparency(1)
    --方向需要逆时针旋转90度
    local newRootCFrame = newBaseCFrame * CFrame.Angles(0, math.rad(-90), 0)
    self:CreateHumanoidRootPart(newRootCFrame)
    
    return self.Model.Car
end

--设置透明度
function M.SetTransparency(self,transparency)
    for _,p in pairs(self.Model:GetDescendants()) do
        if p:IsA("BasePart") then
            p.Transparency=transparency
        end
    end

end

--锚固开关
function M.AnchoredSwitch(self,anchored)
    self.Model.Car.Anchored=anchored
end

function M.GetForwardUnit(self)
    return self.Model.Car:GetPivot().RightVector.Unit
end


local forceBase  = 25
--设置前进速度
function M.SetForwardSpeed(self,speed, carDirect)
   
    -- if not self.Model.Car:FindFirstChild("BodyVelocity") then
    --     local  bodyVelocity = Instance.new("BodyVelocity",self.Model.Car)
    -- end
     if  not carDirect then
        carDirect =  self:GetForwardUnit()
     end
    -- local  
    -- if isDown then
    --     -- 创建一个新的 Vector3，修改 Y 分量
    --     carDirect = Vector3.new(carDirect.X, -20, carDirect.Z)
    -- end
 
    -- self.Model.Car.AlignOrientation.CFrame  = CFrame.lookAt(Vector3.new(0,0,0),carDirect)
    self.Model.Car.VectorForce.Enabled = true
    self.Model.Car.VectorForce.Force = self.Model.Car.VectorForce.Force + carDirect * forceBase* speed *self.Model.Car.AssemblyMass

    -- if isDown then
    --     -- 创建一个新的 Vector3，修改 Y 分量
    --     carDirect = Vector3.new(carDirect.X, -5, carDirect.Z)
    -- end
    -- self.Model.Car.BodyVelocity.velocity =  Vector3.new(carDirect.X*speed,carDirect.Y,carDirect.Z*speed)
    -- self.Model.Car.BodyVelocity.P = math.huge
    -- self.Model.Car.BodyVelocity.maxForce =   Vector3.new(1,1,1)*50000---可以通过调整maxForce的计算方式调整速度增量

     -- print("当前车的推进速度--->",speed,self.Model.Car.BodyVelocity.velocity,self.Model.Car.BodyVelocity)
--       Velocity可以移动您想要移动的部分
--        MaxForce，它可以调整你施加到部件上的各个轴力（X、Y、Z）的大小，以获得其接收Velocity
--          P决定了你将使用多少基本力量来达到预期Velocity目标
end
--添加约束
function M.Weld2Part(self,a,b)
    local weld = Instance.new("WeldConstraint")
	weld.Part0 = a
	weld.Part1 = b
	weld.Parent = a
end

--更新轮胎位置,参数：美术轮胎半径
function M.UpdateRing(self,radius)
    local pivot= self.Car:GetBody():GetEnginePivot()
    local ringSize = self.Model.Car.BL.Size.Y/2
    local newPos=pivot.Position- Vector3.new(0, radius, 0) + Vector3.new(0, ringSize, 0)
    local newBaseCFrame = CFrame.new(newPos) * CFrame.Angles( self.Model:GetPivot():ToEulerAnglesXYZ())
    self.Model:PivotTo(newBaseCFrame) 

    local newRootPos=pivot.Position- Vector3.new(0, radius, 0) + Vector3.new(0, self.CarHumanoidRootPart.Size.Y/2 , 0)
    local newRootCFrame = CFrame.new(newRootPos) * CFrame.Angles( self.Model:GetPivot():ToEulerAnglesXYZ())
    self.CarHumanoidRootPart:PivotTo(newRootCFrame  * CFrame.Angles(0, math.rad(-90), 0)  )
end

--返回美术轮胎约束点
function M.GetRingAtt(self)
   return self.Model.Car.FR.Attachment1
end

-- 返回根节点
function M.GetInstance(self)
    return self.Model.Car
end
-- 返回手动控制的VectorForce
function M.GetForce(self)
    return self.Model.VectorForce
end

function M.GetAlign(self)
    return self.Model.AlignOrientation
end

function M.GetRootAttatchment(self)
    return self.Model.Car.RootAttachment
end
return M